"""add_task_config_and_execution_summary

Revision ID: d585a772ecb9
Revises: c4b517fd89ea
Create Date: 2025-08-04 11:24:44.542696

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision: str = 'd585a772ecb9'
down_revision: Union[str, None] = 'c4b517fd89ea'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    # Add new columns to ai_workflows table
    op.add_column('ai_workflows', sa.Column('task_config', mysql.JSON(), nullable=True, comment='任务配置参数'))
    op.add_column('ai_workflows', sa.Column('execution_summary', mysql.JSON(), nullable=True, comment='执行摘要统计'))
    op.add_column('ai_workflows', sa.Column('expected_outputs', mysql.JSON(), nullable=True, comment='预期输出数量'))
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('news',
    sa.Column('id', mysql.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('title', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=200), nullable=False, comment='新闻标题'),
    sa.Column('content', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=False, comment='新闻内容'),
    sa.Column('image', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=500), nullable=True, comment='新闻图片URL'),
    sa.Column('author_id', mysql.INTEGER(), autoincrement=False, nullable=False, comment='作者ID'),
    sa.Column('created_at', mysql.DATETIME(), nullable=True, comment='创建时间'),
    sa.Column('updated_at', mysql.DATETIME(), nullable=True, comment='更新时间'),
    sa.ForeignKeyConstraint(['author_id'], ['users.id'], name=op.f('news_ibfk_1')),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_unicode_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_news_id'), 'news', ['id'], unique=False)
    op.create_table('images',
    sa.Column('id', mysql.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('entity_id', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=255), nullable=False, comment='图片实体ID'),
    sa.Column('group_id', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=255), nullable=False, comment='图库ID'),
    sa.Column('pic_name', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=255), nullable=False, comment='图片名称'),
    sa.Column('original_filename', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=255), nullable=True, comment='原始文件名'),
    sa.Column('cos_url', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=True, comment='COS访问URL'),
    sa.Column('cos_key', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=500), nullable=True, comment='COS对象键'),
    sa.Column('file_size', mysql.INTEGER(), autoincrement=False, nullable=True, comment='文件大小(字节)'),
    sa.Column('file_type', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=50), nullable=True, comment='文件类型'),
    sa.Column('width', mysql.INTEGER(), autoincrement=False, nullable=True, comment='图片宽度'),
    sa.Column('height', mysql.INTEGER(), autoincrement=False, nullable=True, comment='图片高度'),
    sa.Column('tencent_tags', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=True, comment='腾讯云识别标签(JSON)'),
    sa.Column('custom_content', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=True, comment='自定义内容'),
    sa.Column('upload_status', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=50), nullable=True, comment='上传状态'),
    sa.Column('tencent_response', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=True, comment='腾讯云响应(JSON)'),
    sa.Column('user_id', mysql.INTEGER(), autoincrement=False, nullable=True, comment='用户ID'),
    sa.Column('created_at', mysql.DATETIME(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True, comment='创建时间'),
    sa.Column('updated_at', mysql.DATETIME(), nullable=True, comment='更新时间'),
    sa.Column('deleted_at', mysql.DATETIME(), nullable=True, comment='删除时间'),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_unicode_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_images_id'), 'images', ['id'], unique=False)
    op.create_index(op.f('ix_images_group_id'), 'images', ['group_id'], unique=False)
    op.create_index(op.f('ix_images_entity_id'), 'images', ['entity_id'], unique=False)
    op.create_table('ai_workflows',
    sa.Column('id', mysql.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('user_id', mysql.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('task_name', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=200), nullable=False, comment='任务名称'),
    sa.Column('status', mysql.ENUM('PENDING', 'RUNNING', 'WAITING', 'COMPLETED', 'FAILED', 'CANCELLED'), nullable=False),
    sa.Column('current_step', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=50), nullable=True, comment='当前执行步骤'),
    sa.Column('progress', mysql.INTEGER(), autoincrement=False, nullable=True, comment='进度百分比 0-100'),
    sa.Column('input_data', mysql.JSON(), nullable=True, comment='输入数据'),
    sa.Column('result_data', mysql.JSON(), nullable=True, comment='最终结果数据'),
    sa.Column('task_config', mysql.JSON(), nullable=True, comment='任务配置参数'),
    sa.Column('execution_summary', mysql.JSON(), nullable=True, comment='执行摘要统计'),
    sa.Column('expected_outputs', mysql.JSON(), nullable=True, comment='预期输出数量'),
    sa.Column('error_message', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=True, comment='错误信息'),
    sa.Column('retry_count', mysql.INTEGER(), autoincrement=False, nullable=True, comment='重试次数'),
    sa.Column('created_at', mysql.DATETIME(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_at', mysql.DATETIME(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('started_at', mysql.DATETIME(), nullable=True, comment='开始执行时间'),
    sa.Column('completed_at', mysql.DATETIME(), nullable=True, comment='完成时间'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], name='ai_workflows_ibfk_1'),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_unicode_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_ai_workflows_user_id'), 'ai_workflows', ['user_id'], unique=False)
    op.create_index(op.f('ix_ai_workflows_status'), 'ai_workflows', ['status'], unique=False)
    op.create_index(op.f('ix_ai_workflows_id'), 'ai_workflows', ['id'], unique=False)
    op.create_table('workflow_steps',
    sa.Column('id', mysql.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('workflow_id', mysql.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('step_name', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=100), nullable=False, comment='步骤名称'),
    sa.Column('step_order', mysql.INTEGER(), autoincrement=False, nullable=False, comment='执行顺序'),
    sa.Column('status', mysql.ENUM('PENDING', 'RUNNING', 'WAITING', 'COMPLETED', 'FAILED', 'SKIPPED'), nullable=False),
    sa.Column('external_task_id', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=200), nullable=True, comment='外部API任务ID'),
    sa.Column('external_conversation_id', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=200), nullable=True, comment='外部API对话ID'),
    sa.Column('api_provider', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=50), nullable=True, comment='API提供商 coze/midjourney/flux等'),
    sa.Column('input_data', mysql.JSON(), nullable=True, comment='步骤输入数据'),
    sa.Column('output_data', mysql.JSON(), nullable=True, comment='步骤输出数据'),
    sa.Column('error_message', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=True, comment='错误信息'),
    sa.Column('retry_count', mysql.INTEGER(), autoincrement=False, nullable=True, comment='重试次数'),
    sa.Column('created_at', mysql.DATETIME(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_at', mysql.DATETIME(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('started_at', mysql.DATETIME(), nullable=True, comment='开始执行时间'),
    sa.Column('completed_at', mysql.DATETIME(), nullable=True, comment='完成时间'),
    sa.ForeignKeyConstraint(['workflow_id'], ['ai_workflows.id'], name='workflow_steps_ibfk_1'),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_unicode_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_workflow_steps_workflow_id'), 'workflow_steps', ['workflow_id'], unique=False)
    op.create_index(op.f('ix_workflow_steps_id'), 'workflow_steps', ['id'], unique=False)
    op.create_table('messages',
    sa.Column('id', mysql.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('sender_id', mysql.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('receiver_id', mysql.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('title', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=255), nullable=False),
    sa.Column('content', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=False),
    sa.Column('message_type', mysql.ENUM('USER_TO_ADMIN', 'ADMIN_TO_USER', 'SYSTEM'), nullable=False),
    sa.Column('status', mysql.ENUM('UNREAD', 'READ', 'REPLIED'), nullable=True),
    sa.Column('parent_id', mysql.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('is_deleted', mysql.TINYINT(display_width=1), autoincrement=False, nullable=True),
    sa.Column('created_at', mysql.DATETIME(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
    sa.Column('updated_at', mysql.DATETIME(), nullable=True),
    sa.Column('read_at', mysql.DATETIME(), nullable=True),
    sa.ForeignKeyConstraint(['parent_id'], ['messages.id'], name=op.f('messages_ibfk_3')),
    sa.ForeignKeyConstraint(['receiver_id'], ['users.id'], name=op.f('messages_ibfk_2')),
    sa.ForeignKeyConstraint(['sender_id'], ['users.id'], name=op.f('messages_ibfk_1')),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_unicode_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_messages_id'), 'messages', ['id'], unique=False)
    op.create_table('error_histories',
    sa.Column('id', mysql.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('user_id', mysql.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('workflow_id', mysql.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('step_id', mysql.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('error_type', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=100), nullable=True, comment='错误类型'),
    sa.Column('error_message', mysql.TEXT(collation='utf8mb4_unicode_ci'), nullable=False, comment='错误详情'),
    sa.Column('api_provider', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=50), nullable=True, comment='API提供商'),
    sa.Column('user_name', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=100), nullable=True, comment='用户名'),
    sa.Column('created_at', mysql.DATETIME(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.ForeignKeyConstraint(['step_id'], ['workflow_steps.id'], name=op.f('error_histories_ibfk_3')),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], name=op.f('error_histories_ibfk_1')),
    sa.ForeignKeyConstraint(['workflow_id'], ['ai_workflows.id'], name=op.f('error_histories_ibfk_2')),
    sa.PrimaryKeyConstraint('id'),
    mysql_collate='utf8mb4_unicode_ci',
    mysql_default_charset='utf8mb4',
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_error_histories_workflow_id'), 'error_histories', ['workflow_id'], unique=False)
    op.create_index(op.f('ix_error_histories_user_id'), 'error_histories', ['user_id'], unique=False)
    op.create_index(op.f('ix_error_histories_step_id'), 'error_histories', ['step_id'], unique=False)
    op.create_index(op.f('ix_error_histories_id'), 'error_histories', ['id'], unique=False)
    # ### end Alembic commands ###
